package com.lastproject.ejb.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import com.lastproject.dto.BorrowedDTO;
import com.lastproject.ejb.entities.Book;
import com.lastproject.ejb.entities.Borrowed;
import com.lastproject.ejb.util.BorrowedConversion;

/**
 * Session Bean implementation class BorrowedDAO author: Lia Sincari
 * 
 */
@Stateless
public class BorrowedDAO implements BorrowedDAORemote {

	/**
	 * Default constructor.
	 */
	@PersistenceContext(unitName = "LastProjectEJB")
	private EntityManager em;

	@EJB
	private BorrowedConversion conv;

	public BorrowedDAO() {

	}

	@Override
	public void create(BorrowedDTO borrowedDTO) {
		em.persist(conv.fromDTO(borrowedDTO));
	}

	@Override
	public void edit(BorrowedDTO borrowedDTO) {
		em.merge(conv.fromDTO(borrowedDTO));

	}

	@Override
	public void remove(BorrowedDTO borrowedDTO) {
		Borrowed b = em.find(Borrowed.class, conv.fromDTO(borrowedDTO).getId());
		em.remove(b);

	}

	@Override
	public BorrowedDTO findById(Object id) {
		Borrowed r = em.find(Borrowed.class, id);
		BorrowedDTO borrowed = conv.fromEntity(r);
		return borrowed;
	}

	@Override
	public List<BorrowedDTO> getAll() {
		List<Borrowed> borrowedList = (List<Borrowed>) em.createNamedQuery(
				"Borrowed.findAll", Borrowed.class).getResultList();
		List<BorrowedDTO> borrowedDTO = new ArrayList<BorrowedDTO>();

		for (Borrowed b : borrowedList) {
			BorrowedDTO borrow = new BorrowedDTO();
			borrow = conv.fromEntity(b);
			borrowedDTO.add(borrow);
		}
		return borrowedDTO;
	}

	public List<BorrowedDTO> getRecentBook(int id) {
		List<Borrowed> recentList = em
				.createQuery(
						"SELECT b FROM Borrowed b WHERE b.book.id =:id ORDER BY b.borrowDate DESC",
						Borrowed.class).setParameter("id", id).getResultList();
		List<BorrowedDTO> borrowedDTO = new ArrayList<BorrowedDTO>();
		for (Borrowed b : recentList) {
			BorrowedDTO wl = new BorrowedDTO();
			wl = conv.fromEntity(b);
			borrowedDTO.add(wl);
		}
		return borrowedDTO;
	}

	public List<BorrowedDTO> getByReturnDate(int id) {
		List<Borrowed> recentList = em
				.createQuery(
						"SELECT b FROM Borrowed b WHERE b.book.id =:id and b.returnDate is NULL ",
						Borrowed.class).setParameter("id", id).getResultList();
		List<BorrowedDTO> borrowedDTO = new ArrayList<BorrowedDTO>();

		for (Borrowed b : recentList) {
			BorrowedDTO wl = new BorrowedDTO();
			wl = conv.fromEntity(b);
			borrowedDTO.add(wl);
		}
		return borrowedDTO;
	}

	public List<BorrowedDTO> getAllByBook(int id) {
		List<Borrowed> recentList = em
				.createQuery(
						"SELECT b FROM Borrowed b WHERE b.book.id =:id ORDER BY b.returnDate",
						Borrowed.class).setParameter("id", id).getResultList();
		List<BorrowedDTO> borrowedDTO = new ArrayList<BorrowedDTO>();

		for (Borrowed b : recentList) {
			BorrowedDTO wl = new BorrowedDTO();
			wl = conv.fromEntity(b);
			borrowedDTO.add(wl);
		}
		return borrowedDTO;
	}

	/**
	 * returneza imprumutul curent al unei carti
	 */
	@Override
	public BorrowedDTO findCurrentBorrowOfABook(int id) {

		// select * from borrowed as b where b.idBook=1 and b.RETURNDATE is
		// null;

		List<Borrowed> borroweds = em
				.createQuery(
						"SELECT b FROM Borrowed b WHERE b.book.id = :id and b.returnDate is null",
						Borrowed.class).setParameter("id", id).getResultList();

		if (borroweds.size() > 0)
			return conv.fromEntity(borroweds.get(0));

		return null;
	}

	/**
	 * returneaza borrowed-ul dupa criteriile date (idUser, idBook, today)
	 */
	@Override
	public BorrowedDTO getByIdUserIdBookDate(int idUser, int idBook, Date today) {

		List<Borrowed> borroweds = em
				.createQuery(
						"SELECT b from Borrowed b where b.user.id = :idUser and "
								+ "b.book.id = :idBook and "
								+ "b.borrowDate = :today", Borrowed.class)
				.setParameter("idUser", idUser).setParameter("idBook", idBook)
				.setParameter("today", today).getResultList();
		BorrowedDTO borrowedDTO = null;
		if (borroweds.size() > 0) {
			borrowedDTO = conv.fromEntity(borroweds.get(0));
		}
		return borrowedDTO;
	}

}
